package com.crm.controller;

import com.crm.common.annotation.Log;
import com.crm.common.constant.HttpStatus;
import com.crm.common.enums.BusinessType;
import com.crm.common.result.AjaxResult;
import com.crm.common.result.PageResult;
import com.crm.common.utils.UserThreadLocal;
import com.crm.domain.Business;
import com.crm.domain.Clue;
import com.crm.domain.ClueRecord;
import com.crm.domain.User;
import com.crm.dto.CluePageQueryDto;
import com.crm.dto.FalseClueDto;
import com.crm.dto.GainClueDto;
import com.crm.service.BusinessService;
import com.crm.service.ClueService;
import com.crm.service.ClueRecordService;
import com.crm.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/clues/clue")
public class ClueController {



    private final ClueService clueService;

    private final ClueRecordService clueRecordService;

    private final BusinessService businessService;

    @GetMapping("/list")
    @Log(title = "线索管理", businessType = BusinessType.EXPORT)
    public PageResult page(CluePageQueryDto dto){
        PageResult pageResult = clueService.pageClue(dto);
        return pageResult;
    }

    @PostMapping
    @Log(title = "线索管理", businessType = BusinessType.INSERT)
    public AjaxResult insertClue(@RequestBody Clue clue){
        clueService.insertClue(clue);

        return AjaxResult.success();
    }

    @GetMapping("/{id}")
    @Log(title = "线索管理", businessType = BusinessType.EXPORT)
    public AjaxResult getClueById(@PathVariable Long id){
        return AjaxResult.success(clueService.getById(id));
    }

    @PutMapping
    @Log(title = "线索管理", businessType = BusinessType.UPDATE)
    public AjaxResult updateClue(@RequestBody Clue clue){
        clueService.updateById(clue);
        return AjaxResult.success();
    }

    @GetMapping("/pool")
    public AjaxResult pageCluePool(CluePageQueryDto dto){
        return clueService.pageCluePool(dto);
    }

    @PutMapping("/gain")
    public AjaxResult gainClue(@RequestBody GainClueDto dto){
        return clueService.gainClue(dto);
    }

    @PutMapping("/false/{id}")
    public AjaxResult falseClue(@PathVariable Long id, @RequestBody FalseClueDto dto){
        Clue clue = clueService.getById(id);
        clue.setStatus(Clue.CLUE_STATUS_FALSE);
        clue.setFalseCount(clue.getFalseCount()+Clue.CLUE_FALSE_INCREMENT);
        ClueRecord clueRecord = new ClueRecord();
        clueRecord.setClueId(clue.getId());
        clueRecord.setFalseReason(dto.getReason());
        clueRecord.setRecord(dto.getRemark());
        clueRecord.setName(clue.getName());
        clueRecord.setLevel(clue.getLevel().toString());
        clueRecord.setSubject(clue.getSubject());
        clueRecord.setNextTime("null");
        clueRecord.setType("1");
        clueRecordService.save(clueRecord);
        clueService.updateById(clue);
        return AjaxResult.success();
    }

    @PutMapping("/changeBusiness/{id}")
    public AjaxResult transferClue(@PathVariable Long id){
        //todo 转商机
        Business business = new Business();
        businessService.addBusiness(business);
        return AjaxResult.success();
    }

}
